﻿Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging

Partial Public Class FollowingUserCard
    Inherits UserControl

    Private ReadOnly _userAvatarUrlBase As String = NavigationData.SERVER_ACCESS_URL + "/vdm/api/file/download/"
    Private ReadOnly _userAvatarDefaultUrl As String = "/VMDWebFrontend;component/Assets/SPGLP_icon.jpg"

    Private _client As HttpClientBase

    Public Sub New()
        Me.InitializeComponent()
        _client = New HttpClientBase()
    End Sub

    Private Sub BUT_ToUserPage_Click(sender As Object, e As RoutedEventArgs)
        Dim user = CType(Me.DataContext, User)
        If user IsNot Nothing Then
            NavigationService.Navigate(String.Format("/View/UserPage.xaml?id={0}", user.Id))
        End If
    End Sub

    Private Sub BUT_UnFollow_Click(sndr As Object, e As RoutedEventArgs)
        If MessageBox.Show("您确定要取消关注该用户吗？", MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
            Call UnFollowUser()
        End If
    End Sub

    Private Async Function UnFollowUser() As Task
        Try
            Dim userId = CType(Me.DataContext, User).Id
            Dim response = Await _client.DeleteAsync(Of Boolean)(String.Format("vdm/api/following/unfollow/{0}/{1}", Application.Current.Resources("UserId").ToString(), userId))
            If response.Code <> 200 OrElse response.Data = False Then
                    MessageBox.Show("取消关注失败：" + response.Message)
                    Return
                End If
            MessageBox.Show("取消关注成功，请刷新页面")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("取消关注失败：" + ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
            Return
        End Try
    End Function

    Public Sub AvatarLoad()
        If Me.DataContext IsNot Nothing Then
            Dim userAvatarName = CType(Me.DataContext, User).Avatar
            Dim userAvatar As BitmapImage = Nothing
            If String.IsNullOrEmpty(userAvatarName) Then
                userAvatar = New BitmapImage(New Uri(_userAvatarDefaultUrl, UriKind.Relative))
            Else
                userAvatar = New BitmapImage(New Uri(_userAvatarUrlBase + userAvatarName, UriKind.Absolute))
            End If
            Me.IMG_Avatar.Source = userAvatar
        End If
    End Sub
End Class
